<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\facade\Request;

class KeynoteBook extends Controller
{
    public function index()
    {
        if (Request::isAjax()) {
            $pageSize   = input('limit') ? input('limit') : config('pageSize');
            $sort_by    = input('sort_order') ? input('sort_by') : 'id';
            $sort_order = input('sort_order') ? input('sort_order') : 'asc';
            $list       = db('keynote_book')->order($sort_by.' '.$sort_order)
                ->paginate($pageSize)
                ->toArray();

            return $result = ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        } else {
            return $this->fetch();
        }
    }

    /**
     * 添加.
     *
     * @return mixed
     *
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/20 17:17
     */
    public function add()
    {
        if (request()->isPost()) {
          
            $insert = [
                'name'        => input('name'),
                'image'       => input('image'),
                'description' => input('description'),
                'createtime'  => time(),
                'updatetime'  => time(),
            ];
            $m=db('keynote_book');
            $m->insert($insert);
       
            $result['msg']  = '添加成功!';
            $result['code'] = 1;

            return $result;
        } else {
            $this->assign('title', lang('add'));
            $this->assign('info', 'null');

            return $this->fetch('form');
        }
    }

    /**
     * 启用/禁用.
     *
     * @return array
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     *
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/20 17:17
     */
    public function modulestate()
    {
        $id     = input('post.id');
        $status = input('post.status');
        if (false !== db('keynote_book')->where('id='.$id)->update(['status' => $status])) {
            return ['status' => 1, 'msg' => '设置成功!'];
        } else {
            return ['status' => 0, 'msg' => '设置失败!'];
        }
    }

    /**
     * 删除.
     *
     * @return array
     *
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     *
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/20 17:18
     */
    public function del()
    {
        $id = input('param.id');
        $r  = db('keynote_book')->find($id);
        if (!empty($r)) {
            db('keynote_book')->delete($id);
        }

        return ['code' => 1, 'msg' => '删除成功！'];
    }

    /**
     * 编辑.
     *
     * @author zhanglibo <396223143@qq.com>
     * @date   2019/10/20 17:19
     */
    public function edit()
    {
        if (request()->isPost()) {
            $data = Request::except('file');
         
            if (false !== db('keynote_book')->update($data)) {
                return ['code' => 1, 'url' => url('index'), 'msg' => '修改成功!'];
            } else {
                return ['code' => 0, 'url' => url('index'), 'msg' => '修改失败!'];
            }
        } else {
            $map['id'] = input('param.id');
            $info      = db('keynote_book')->field('id,name,description,image')->where($map)->find();
            $this->assign('title', lang('edit'));
            $this->assign('info', json_encode($info, true));
      
            return $this->fetch('form');
        }
    }

    public function upload(){
        if (request()->isPost()) {
            $data = Request::except('file');
          
            $image=$data['image'];
            db('keynote_book_file')->where(['main_id'=>$data['main_id']])->delete();

            foreach ($image as $item){
                $init[]=[
                    'main_id'=>$data['main_id'],
                    "path"=>$item,
                    'createtime'=>time()
                ];
              
            }

            if (false !==  db('keynote_book_file')->insertall($init)) {
                return ['code' => 1, 'url' => url('index'), 'msg' => '修改成功!'];
            } else {
                return ['code' => 0, 'url' => url('index'), 'msg' => '修改失败!'];
            }
        } else {        
            $this->assign('title', '上传图片');

            $where['main_id']=input('param.id');
            $image      = db('keynote_book_file')->field('path')->where($where)->select();
            $image=array_column($image,'path');
           
            $this->assign('image',$image);
            $this->assign('info',json_encode($where,1));

            return $this->fetch('upload');
        }
    }
}
